package and.example.addresscontacts;

import java.util.concurrent.atomic.AtomicBoolean;

import and.example.interfaces.IArguments;
import android.app.Activity;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.Toast;

public class SendSMSActivity extends Activity implements IArguments {

	EditText textPhoneNo;
	EditText textSMS;
	private Button button;

	String SENT = "SMS_SENT";
	String DELIVERED = "SMS_DELIVERED";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.send_sms);

		textPhoneNo = (EditText) findViewById(R.id.editTextPhoneNo);
		textSMS = (EditText) findViewById(R.id.editTextSMS);
		String textNo = getIntent().getStringExtra(COLUMNS[3]);

		if (textNo == null) {
			textNo = "Please give phone number";
		}

		textPhoneNo.setText(textNo);

		Button buttonSend = (Button) findViewById(R.id.buttonSend);
		buttonSend.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

				String phoneNo = textPhoneNo.getText().toString();
				String message = textSMS.getText().toString();
				if (phoneNo.length() > 0 && message.length() > 0)
					sendSMS(phoneNo, message);
				else
					Toast.makeText(getBaseContext(),
							"Please enter both phone number and message.",
							Toast.LENGTH_SHORT).show();
			}
		});

		RelativeLayout layout = (RelativeLayout) findViewById(R.id.header_layout);
		Button btn = (Button) layout.findViewById(R.id.header_right_btn);
		btn.setOnClickListener(new android.view.View.OnClickListener() {
			@Override
			public void onClick(View v) {
				SendSMSActivity.this.finish();
			}
		});

		button = (Button) findViewById(R.id.btnCaptialize);
		button.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				String string = textSMS.getText().toString();
				textSMS.setText(string.toUpperCase());
			}
		});
	}

	// public void onclick(View v) {
	//
	// String sms = textSMS.getText().toString();
	// String phoneNo = textPhoneNo.getText().toString();
	//
	// try {
	//
	// SmsManager manager = SmsManager.getDefault();
	// manager.sendTextMessage(phoneNo, null, sms, null, null);
	// Toast.makeText(this, "SMS sent!", Toast.LENGTH_LONG).show();
	//
	//
	// } catch (Exception e) {
	// Toast.makeText(this, e.getMessage(), Toast.LENGTH_LONG).show();
	// }
	// }

	private void sendSMS(String phoneNumber, String message) {

		PendingIntent sentPI = PendingIntent.getBroadcast(this, 0, new Intent(
				SENT), 0);

		PendingIntent deliveredPI = PendingIntent.getBroadcast(this, 0,
				new Intent(DELIVERED), 0);

		isSentSucces();
		isSentDelivered();

		SmsManager sms = SmsManager.getDefault();
		sms.sendTextMessage(phoneNumber, null, message, sentPI, deliveredPI);
	}

	/**
	 * TODO --- When the SMS has been sent ---
	 * 
	 * @return
	 */
	public boolean isSentSucces() {

		// Boolean boolean1 = new Boolean(false);
		final AtomicBoolean atomicBoolean = new AtomicBoolean();

		registerReceiver(new BroadcastReceiver() {

			@Override
			public void onReceive(Context arg0, Intent arg1) {
				switch (getResultCode()) {
				case Activity.RESULT_OK:
					Toast.makeText(getBaseContext(), "SMS sent",
							Toast.LENGTH_SHORT).show();
					atomicBoolean.set(true);

					break;
				case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
					Toast.makeText(getBaseContext(), "Generic failure",
							Toast.LENGTH_SHORT).show();
					atomicBoolean.set(false);
					break;
				case SmsManager.RESULT_ERROR_NO_SERVICE:
					Toast.makeText(getBaseContext(), "No service",
							Toast.LENGTH_SHORT).show();
					atomicBoolean.set(false);
					break;
				case SmsManager.RESULT_ERROR_NULL_PDU:
					Toast.makeText(getBaseContext(), "Null PDU",
							Toast.LENGTH_SHORT).show();
					atomicBoolean.set(false);
					break;
				case SmsManager.RESULT_ERROR_RADIO_OFF:
					Toast.makeText(getBaseContext(), "Radio off",
							Toast.LENGTH_SHORT).show();
					atomicBoolean.set(false);
					break;
				}
			}
		}, new IntentFilter(SENT));

		return atomicBoolean.get();
	}

	/**
	 * TODO--- When the SMS has been delivered. ---
	 * 
	 * @return
	 */
	public boolean isSentDelivered() {

		final AtomicBoolean atomicBoolean = new AtomicBoolean(false);

		registerReceiver(new BroadcastReceiver() {
			@Override
			public void onReceive(Context arg0, Intent arg1) {
				switch (getResultCode()) {

				case Activity.RESULT_OK:
					Toast.makeText(getBaseContext(), "SMS delivered",
							Toast.LENGTH_SHORT).show();
					atomicBoolean.set(true);
					break;

				case Activity.RESULT_CANCELED:
					Toast.makeText(getBaseContext(), "SMS not delivered",
							Toast.LENGTH_SHORT).show();
					atomicBoolean.set(false);
					break;
				}
			}
		}, new IntentFilter(DELIVERED));

		return atomicBoolean.get();

	}

}
